// ==UserScript==
// @name         南京人设局专技继续教育视频挂机
// @namespace    https://gitee.com/WOAICHIYUZHOU/static-file/raw/master/mynjOnlineSource.js
// @downloadURL	 https://gitee.com/WOAICHIYUZHOU/static-file/raw/master/mynjOnlineSource.js
// @updateURL    https://gitee.com/WOAICHIYUZHOU/static-file/raw/master/mynjOnlineSource.js
// @version      1.0.161208
// @description  进入视频课程之后自动学习视频。1. 10分钟一次的确认播放问题；2. 自动学习下一个视频 
// @description  注意： 每次学完一个课程得手动选择另一个课程 
// @author       pangHu
// @match        https://m.mynj.cn:11188/*/play/player*
// @grant        none
// @time		 2021-9-16 12:08
// ==/UserScript==
(function () {
	'use strict';

	function getUnsafeWindow() {
		if (this) {
			console.log(this);
			if (typeof (this.unsafeWindow) !== "undefined") { //Greasemonkey, Scriptish, Tampermonkey, etc.
				return this.unsafeWindow;
			} else if (typeof (unsafeWindow) !== "undefined" && this === window && unsafeWindow === window) { //Google Chrome natively
				var node = document.createElement("div");
				node.setAttribute("onclick", "return window;");
				return node.onclick();
			} else { }
		} else { //Opera, IE7Pro, etc.
			return window;
		}
	}
	var myUnsafeWindow = getUnsafeWindow();
	var doc = myUnsafeWindow.document;
	var processTimer = null;
	myUnsafeWindow.clearInterval(processTimer);
	// 每隔5秒检测一次
	processTimer = myUnsafeWindow.setInterval(TimeProcess, 5000);

	// 检测函数
	function TimeProcess() {
		// 处理弹出框问题
		var messageButton = $('a.l-btn.l-btn-small');
		if (messageButton.length > 0) {
			for (var i = 0; i < messageButton.length; i++) {
				$('a.l-btn.l-btn-small')[i].click();
			}
		}
		var finished = $('td.learnpercent>span>span');
		// 学习进度
		if (finished.length > 0) {
			if (finished[0].innerText.indexOf('已完成') != -1) {
				// 已完成
				var list = $('ul.ztree>li>ul>li>a');
				if (list.length == 0) {
					list = $('ul.ztree>li>a');
				}
				if (list.length > 0) {
					console.log('a.list', list)
					for (var i = 0; i < list.length; i++) {
						if (list[i].outerText.indexOf('点击学习') != -1 && list[i].outerText.indexOf('学习中') == -1) {
							console.log('%c 进入下一个学习 %s', 'color:green', list[i].innerText);
							list[i].click();
							return
						}
					}
					myUnsafeWindow.clearInterval(processTimer)
					if (confirm('(*^▽^*)当前目录已学习完毕~~~~ \r\n 返回我的课程？')) {
						$('a.backClass')[0].click()
					}
				}
			} else {
				// 系统学习进度未完成 
				if (!window.p) {
					// 播放器加载失败 刷新页面
					location.reload();
					return
				}
				if ($('button.vjs-big-play-button').css('display') != 'none') {
					$('button.vjs-big-play-button')[0].click()
					$('.vjs-big-play-button').click();
					window.p.bigPlayButton.handleClick()
				}
				let learnpercent = parseInt(finished[0].innerText)
				let totalTime = window.p.currentTime() + window.p.remainingTime();
				// 处理30s会更新一次学习时间 学习进度差距大
				let currentTime = window.p.currentTime() > 30 ? window.p.currentTime() - 30 : window.p.currentTime()
				let currentPre = Math.floor(currentTime / totalTime * 100);
				// console.log('%c currentPre %s %c learnpercent  %s', 'color:blue;', currentPre, 'color:green;', learnpercent);
				if (learnpercent > 0 && currentPre > learnpercent) {
					// 设置播放器时间
					console.log('修改当前播放时间!', learnpercent * totalTime / 100)
					window.p.currentTime(learnpercent * totalTime / 100)
				}
				if (window.p && $('.menuBody')[0].clientHeight > 100) {
					try {
						if (window.p.paused() == true) {
							window.p.play();
						} else {
							window.p.volume(0);
						}
					} catch (e) {
						location.reload();
					}
					// $('button.vjs-big-play-button').click();
				}
			}
		}

	}

})();
